<?php

namespace Web\MainSiteBundle\Controller;

use Model\MessageBundle\Model\MessageModel;
use Model\SchoolBundle\Entity\School;
use Model\SchoolBundle\Model\SchoolModel;
use Model\TeacherBundle\Entity\Teacher;
use Model\TeacherBundle\Entity\TeacherApplySchool;
use Model\TeacherBundle\Model\TeacherApplySchoolModel;
use Model\TeacherBundle\Model\TeacherModel;
use Model\UserBundle\Model\UserModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/School/")
 */
class SchoolManageController extends WebController
{
    /**
     * @route("edit",name="WebSchoolEdit")
     */
    public function WebSchoolEdit(Request $request)
    {
        $user = $this->needToBeUser();
        $userId = $user->getId();
        $schoolModel = new SchoolModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {
            if (!$id = $this->getParamRequired("id")) {return $this->js_out("请刷新页面后重试");}
            if (!$intro = $this->getParamRequired("intro")) {return $this->js_out("请填写学校简介");}
            if ($logo = $this->getParamRequired("newLogo")) {
                $schoolInfo["logo"] = $logo;
            }
            $schoolInfo["intro"] = $intro;
            $schoolModel->commUpdate($id,$schoolInfo);
        }

        //获取学校信息
        $schoolInfo = $schoolModel->getByUserId($userId);
        $data["school"] = $schoolInfo;
        return $this->render("WebMainSiteBundle:School:info.html.twig",$data);

    }

    /**
     * @route("teacher/apply",name="WebSchoolTeacherApply")
     */
    public function WebSchoolTeacherApply(Request $request)
    {
        $user = $this->needToBeUser();
        $schoolModel = new SchoolModel($this->getDoctrine());
        $school = $schoolModel->getByUserId($user->getId());
        $this->needToBeSchoolOwner($school["id"]);
        $teacherApplySchoolModel = new TeacherApplySchoolModel($this->getDoctrine());
        $applys = $teacherApplySchoolModel->getBySchoolId($school["id"]);
        $teacherModel = new TeacherModel($this->getDoctrine());
        foreach ($applys as &$apply) {
            $apply["user"] = $user;
            $apply["teacher"] = $teacherModel->getById($apply["teacherId"]);
        }
        $data["applys"] = $applys;
        return $this->render("WebMainSiteBundle:School:teacher_apply.html.twig",$data);
    }


    /**
     * @route("teacher/apply/deny/{id}",name="WebSchoolTeacherApplyDeny")
     */
    public function WebSchoolTeacherApplyDeny(TeacherApplySchool $teacherApplySchool)
    {
        $schoolId = $teacherApplySchool->getSchoolId();
        $this->needToBeSchoolOwner($schoolId);
        if (!$reply = $this->getParamRequired("reply")) {
            return $this->js_out("请填写拒绝原因");
        }
        $teacherApplySchool->setStatus(2);
        $teacherApplySchool->setReply($reply);
        $em = $this->getDoctrine()->getManager();
        $em->persist($teacherApplySchool);
        $em->flush();

        //发消息
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacher = $teacherModel->getById($teacherApplySchool->getTeacherId());
        $schoolModel = new SchoolModel($this->getDoctrine());
        $school = $schoolModel->getById($teacherApplySchool->getSchoolId());
        $schoolName = $school["name"];
        $messageModel = new MessageModel($this->getDoctrine());
        $params["title"] = "你的学校合作申请被拒绝了，学校： $schoolName";
        $params["content"] = "学校 $schoolName 拒绝与您合作，拒绝理由：$reply";
        $params["toWho"] = $teacher["userId"];
        $messageModel->commAdd($params);
        return $this->redirectToRoute("WebSchoolTeacherApply");
    }
    /**
     * @route("teacher/apply/accept/{id}",name="WebSchoolTeacherApplyAccept")
     */
    public function WebSchoolTeacherApplyAccept(TeacherApplySchool $teacherApplySchool)
    {
        $schoolId = $teacherApplySchool->getSchoolId();
        $this->needToBeSchoolOwner($schoolId);

        //设置申请状态为通过
        $teacherApplySchool->setStatus(1);
        $em = $this->getDoctrine()->getManager();
        $em->persist($teacherApplySchool);
        //设置教师属性
        $rep = $this->getDoctrine()->getRepository("ModelTeacherBundle:Teacher");
        $teacher = $rep->find($teacherApplySchool->getTeacherId());
        $teacher->setSchool($schoolId);
        $em->persist($teacher);
        $em->flush();
        //发消息
        $messageModel = new MessageModel($this->getDoctrine());
        $params["title"] = "学校合作申请已通过";
        $params["content"] = "您的加入学校的合作申请已通过，即日起您已正式加入该学校，您发布的所有课程都会纳入该校课程";
        $params["toWho"] = $teacher->getUserId();
        $messageModel->commAdd($params);
        return $this->redirectToRoute("WebSchoolTeacherApply");
    }


    /**
     * @route("teachers",name="WebSchoolTeachers")
     */
    public function WebSchoolTeachers()
    {
        $user = $this->needToBeUser();
        $schoolModel = new SchoolModel($this->getDoctrine());
        $school = $schoolModel->getByUserId($user->getId());
        $this->needToBeSchoolOwner($school["id"]);
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teachers = $teacherModel->getBySchoolId($school["id"]);
        $userModel = new UserModel($this->getDoctrine());
        foreach ($teachers as &$teacher) {
            $teacher["user"] = $userModel->getUserInfo($teacher["userId"]);
        }
        $data["teachers"] = $teachers;
        return $this->render("WebMainSiteBundle:School:teachers.html.twig",$data);
    }


    /**
     * @route("teacher/remove",name="WebSchoolTeacherRemove")
     */
    public function WebSchoolTeacherRemove()
    {
        if (!$teacherId = $this->getParamRequired("id")) {return $this->msg_out("请刷新页面后重试");}
        if (!$msg = $this->getParamRequired("msg")) {return $this->msg_out("请说明解除原因");}
        $user = $this->needToBeSchoolManager();
        $schoolModel = new SchoolModel($this->getDoctrine());
        $school = $schoolModel->getByUserId($user->getId());
        $this->needToBeSchoolOwner($school["id"]);
        $schoolModel->removeTeacher($school["id"],$teacherId);
        //发消息
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacher = $teacherModel->getById($teacherId);
        $schoolName = $school["name"];
        $messageModel = new MessageModel($this->getDoctrine());
        $params["title"] = "你与学校 $schoolName 的合作申请已通过";
        $params["content"] = "学校 $schoolName 解除了与您的合作关系，解除理由：$msg";
        $params["toWho"] = $teacher["userId"];
        $messageModel->commAdd($params);
        return $this->msg_out("关系已解除",$this->generateUrl("WebSchoolTeachers"));
    }


    /**
     * @route("home/{id}",name="WebSchoolHome")
     */
    public function WebSchoolHome(School $school)
    {
        $data = array();
        $data["school"] = $school;
        return $this->render("WebMainSiteBundle:School:home.html.twig",$data);
    }

}
